//
//  UIButton+Extension.swift
//  上海微博
//
//  Created by zhangliming on 16/2/23.
//  Copyright © 2016年 zhangliming. All rights reserved.
//

import UIKit

extension UIButton {

    class func zh_UIButton (imageName imageName:String? = nil, bgImageName:String? = nil, title:String = "",titleColor:UIColor = UIColor.redColor()) -> UIButton {

        let composeButton = UIButton()

        //设置按钮图片

        if let imgName = imageName {

            var image = UIImage(named: imgName)

            var imageSel = UIImage(named: imgName + "_highlighted")

            ///图片拉伸

            image = image?.stretchableImageWithLeftCapWidth(Int ((image?.size.width)! * 0.5), topCapHeight:Int ((image?.size.height)! * 0.5))

            imageSel = imageSel?.stretchableImageWithLeftCapWidth(Int((imageSel?.size.width)! * 0.5), topCapHeight: Int((imageSel?.size.height)! * 0.5))

            ///设置图片

            composeButton.setImage(image, forState: .Normal)

            composeButton.setImage(image, forState: .Highlighted)

        }

        if let backImageName = bgImageName {

            ///图片拉伸

            var bgImage = UIImage(named: backImageName)

            var bgImageSel = UIImage(named: backImageName + "_highlighted")

            bgImage = bgImage?.stretchableImageWithLeftCapWidth(Int(bgImage!.size.width * 0.5), topCapHeight: Int((bgImage?.size.height)! * 0.5))

            bgImageSel = bgImageSel?.stretchableImageWithLeftCapWidth(Int(bgImageSel!.size.width * 0.5), topCapHeight: Int((bgImageSel?.size.height)! * 0.5))

            ///设置图片

            composeButton.setBackgroundImage(bgImage, forState: .Normal)

            composeButton.setBackgroundImage(bgImageSel, forState: .Highlighted)

        }

        composeButton.setTitle(title, forState: .Normal)
        
        composeButton.setTitleColor(titleColor, forState: .Normal)
        
        composeButton.sizeToFit()
        
        return composeButton
        
    }
}